package com.algebra.util.sensitive;

/**
 * @author al
 * @date 2022/5/25 14:05
 * @description
 */
public class SensitiveStrUtil {


    public static String positiveString(String subVal, int srvValLen, String markStr) {
        if (subVal == null) {
            return null;
        } else {
            if (isEmptyString(markStr)) {
                markStr = " ";
            }
            int markStrLen = markStr.length();
            int subValLen = subVal.length();
            int lenGap = srvValLen - subValLen;
            if (lenGap <= 0) {
                return subVal;
            } else if (markStrLen == 1 && lenGap <= 8192) {
                return getSensitiveStr(subVal, srvValLen, markStr.charAt(0));
            } else if (lenGap == markStrLen) {
                return subVal.concat(markStr);
            } else if (lenGap < markStrLen) {
                return subVal.concat(markStr.substring(0, lenGap));
            } else {
                char[] gapChars = new char[lenGap];
                char[] markChars = markStr.toCharArray();
                for (int i = 0; i < lenGap; ++i) {
                    gapChars[i] = markChars[i % markStrLen];
                }
                return subVal.concat(new String(gapChars));
            }
        }
    }

    public static String reverseString(String subVal, int srvValLen, String markStr) {
        if (subVal == null) {
            return null;
        } else {
            if (isEmptyString(markStr)) {
                markStr = " ";
            }
            int markStrLen = markStr.length();
            int subValLen = subVal.length();
            int lenGap = srvValLen - subValLen;
            if (lenGap <= 0) {
                return subVal;
            } else if (markStrLen == 1 && lenGap <= 8192) {
                return getSensitiveStr2(subVal, srvValLen, markStr.charAt(0));
            } else if (lenGap == markStrLen) {
                return markStr.concat(subVal);
            } else if (lenGap < markStrLen) {
                return markStr.substring(0, lenGap).concat(subVal);
            } else {
                char[] gapChars = new char[lenGap];
                char[] markChars = markStr.toCharArray();
                for (int i = 0; i < lenGap; ++i) {
                    gapChars[i] = markChars[i % markStrLen];
                }
                return new String(gapChars).concat(subVal);
            }
        }
    }

    public static String mobile(String var0, String var1) {
        if (!isEmptyString(var0) && !isEmptyString(var1)) {
            return var0.startsWith(var1) ? var0.substring(var1.length()) : var0;
        } else {
            return var0;
        }
    }

    public static String subStringToIdx(String value, int idx) {
        if (value == null) {
            return null;
        } else if (idx < 0) {
            return "";
        } else {
            return value.length() <= idx ? value : value.substring(0, idx);
        }
    }

    public static String subStringFromIdx(String value, int idx) {
        if (value == null) {
            return null;
        } else if (idx < 0) {
            return "";
        } else {
            return value.length() <= idx ? value : value.substring(value.length() - idx);
        }
    }

    public static boolean isWhitespace(CharSequence charSequence) {
        int charLength = getCharLength(charSequence);
        if (charLength == 0) {
            return true;
        } else {
            for (int i = 0; i < charLength; ++i) {
                if (!Character.isWhitespace(charSequence.charAt(i))) {
                    return false;
                }
            }
            return true;
        }
    }

    public static String getSensitiveStr(String subVal, int srvValLen, char markChar) {
        if (subVal == null) {
            return null;
        } else {
            int lenGap = srvValLen - subVal.length();
            if (lenGap <= 0) {
                return subVal;
            } else {
                return lenGap > 8192 ? positiveString(subVal, srvValLen, String.valueOf(markChar)) : subVal.concat(getGapStr(markChar, lenGap));
            }
        }
    }
    public static String getSensitiveStr2(String subVal, int srvValLen, char markChar) {
        if (subVal == null) {
            return null;
        } else {
            int lenGap = srvValLen - subVal.length();
            if (lenGap <= 0) {
                return subVal;
            } else {
                return lenGap > 8192 ? reverseString(subVal, srvValLen, String.valueOf(markChar)) : getGapStr(markChar, lenGap).concat(subVal);
            }
        }
    }

    public static String email(String srcVal, int emailMarkIdx, int srcValLen) {
        if (srcVal == null) {
            return null;
        } else if (srcValLen >= 0 && emailMarkIdx <= srcVal.length()) {
            if (emailMarkIdx < 0) {
                emailMarkIdx = 0;
            }
            return srcVal.length() <= emailMarkIdx + srcValLen ? srcVal.substring(emailMarkIdx) : srcVal.substring(emailMarkIdx, emailMarkIdx + srcValLen);
        } else {
            return "";
        }
    }


    /**
     * 按照个数填充占位符
     * @param markChar 占位符
     * @param lenGap 数目
     */
    public static String getGapStr(char markChar, int lenGap) {
        if (lenGap <= 0) {
            return "";
        } else {
            char[] gapChars = new char[lenGap];
            for(int i = lenGap - 1; i >= 0; --i) {
                gapChars[i] = markChar;
            }
            return new String(gapChars);
        }
    }

    public static int getCharLength(CharSequence charSequence) {
        return charSequence == null ? 0 : charSequence.length();
    }

    public static boolean isEmptyString(CharSequence charSequence) {
        return charSequence == null || charSequence.length() == 0;
    }


    public static int getCharIndex(String value, String charStr) {
        return isEmptyString(value) ? -1 : value.indexOf(charStr);
    }


}
